home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / SAT 2.3.7 / Demos / Demo ƒ / StepPlatform Demo ƒ / sPlayerSprite.p < prev    next >
Encoding:
Text File  |  1995-09-03  |  4.4 KB  |  179 lines  |  [TEXT/PJMM]

  1. (* Player sprite for the Platform test *)
  2.  
  3. unit sPlayerSprite;
  4. interface
  5.     uses
  6. {$ifc UNDEFINED THINK_PASCAL}
  7.         Types, QuickDraw, Menus, Windows, TextEdit, Fonts, Dialogs, Memory, ToolUtils, {}
  8. {$endc}
  9.         SAT, PlatformGlobals;
  10.  
  11.     procedure InitPlayerSprite;
  12.     procedure SetupPlayerSprite (me: PlSpritePtr);
  13.     procedure HandlePlayerSprite (me: PlSpritePtr);
  14.  
  15.     var
  16.         playerSp: PlSpritePtr;
  17.  
  18. implementation
  19.  
  20. {ifndef abs}
  21. {abs=(x)    (x>0?x:-x);}
  22. {endif}
  23.  
  24.     const
  25.         PlayerHigh = 64;
  26.         PlayerSpeed = 5;
  27.         Gforce = 2;{like g = 9.8 m/sec^2}
  28.         JumpSpeed = 23;
  29.  
  30.     var
  31.         rightFaces, leftFaces: array[0..4] of FacePtr;
  32.         fallFace: array[0..2] of FacePtr;
  33.  
  34.     procedure HandleKeys (me: PlSpritePtr);
  35.     forward;
  36.  
  37.     procedure InitPlayerSprite;
  38.         var
  39.             i: Integer;
  40.     begin
  41.         for i := 0 to 3 do
  42.             begin
  43.                 leftFaces[i] := SATGetFace(128 + i);
  44.                 rightFaces[i] := SATGetFace(132 + i);
  45.             end;
  46.         for i := 0 to 1 do
  47.             fallFace[i] := SATGetFace(136 + i);
  48.     end;
  49.  
  50.     procedure HandlePlayerSprite (me: PlSpritePtr);
  51.         var
  52.             theEvent: EventRecord;
  53.             temp: char;
  54.     begin
  55.         HandleKeys(me);
  56.  
  57.         (*  Vspeed Limit ,Acceleration and Updating V Position    *)
  58.         if (me^.speed.v > 30) then
  59.             me^.speed.v := 30;
  60.         me^.speed.v := me^.speed.v + Gforce;
  61.         me^.position.v := me^.position.v + me^.speed.v;
  62.  
  63.         (*    Keeping the Hero with the same speed as the HMov platform    *)
  64.         if (me^.action = JumpFromHMov) then
  65.             me^.position.h := me^.position.h + me^.speed.h;
  66.  
  67.         (*Make sure we are always visible!*)
  68.         (*************************)
  69.         if (me^.position.v < 0) then
  70.             begin
  71.                 me^.position.v := 0;
  72.                 me^.speed.v := 0;
  73.             end;
  74.         if (me^.position.h < 0) then
  75.             begin
  76.                 me^.position.h := 0;
  77.                 me^.speed.h := 0;
  78.             end;
  79.         if (me^.position.v > gSAT.offSizeV - PlayerHigh) then
  80.             begin
  81.                 me^.action := Stand;
  82.                 me^.position.v := gSAT.offSizeV - PlayerHigh;
  83.                 me^.speed.v := 0;
  84.             end;
  85.         if (me^.position.h > gSAT.offSizeH - PlayerHigh) then
  86.             begin
  87.                 me^.position.h := gSAT.offSizeH - PlayerHigh;
  88.                 me^.speed.h := 0;
  89.             end;
  90.  
  91.         me^.mode := abs(me^.mode + 1);
  92.         me^.layer := -(me^.position.v);
  93.     end; {HandlePlayerSprite}
  94.  
  95.  
  96.     procedure HitPlayerSprite (me: SpritePtr; him: SpritePtr);
  97.     begin
  98. (* Hit something! We can take whatever action we need here, but in this case,}
  99. {     we let the other sprites decide. We could have omitted this function altogether and passed nil.*)
  100.         if him^.kind = 1 then
  101.             begin
  102.             end
  103.         else if him^.kind = 2 then
  104.             begin
  105.             end;
  106.     end; {HitPlayerSprite}
  107.  
  108.     procedure SetupPlayerSprite (me: PlSpritePtr);
  109.     begin
  110.         me^.action := Stand;
  111.         me^.mode := 0;
  112.         me^.speed.h := 0;
  113.         me^.kind := 1;                                  (* friend kind *)
  114.         SetRect(me^.hotRect, 12, 0, PlayerHigh - 10, PlayerHigh);
  115.         me^.face := fallFace[0];
  116.         me^.task := @HandlePlayerSprite;
  117.         me^.hitTask := @HitPlayerSprite;
  118.  
  119.         playerSp := me;
  120.     end; {SetupPlayerSprite}
  121.  
  122.     procedure HandleKeys (me: PlSpritePtr);
  123.         {    RIGHT - Key Number 6}
  124.     begin
  125.         if (IsPressed($7B) or IsPressed($56)) then        (* left *)
  126.             begin
  127.                 if me^.action = JumpFromHMov then
  128.                     me^.position.h := me^.position.h - PlayerSpeed - abs(me^.speed.h)
  129.                 else
  130.                     me^.position.h := me^.position.h - PlayerSpeed;
  131.                 me^.speed.h := -PlayerSpeed;
  132.                 me^.face := leftFaces[BitAnd(me^.mode div 3, 3)];
  133.             end;
  134.  
  135.         {    LEFT    - Key Number 4}
  136.         if (IsPressed($7C) or IsPressed($58)) then    (* right *)
  137.             begin
  138.                 if (me^.action = JumpFromHMov) then    {stay on the platform}
  139.                     me^.position.h := me^.position.h + PlayerSpeed - abs(me^.speed.h)
  140.                 else
  141.                     me^.position.h := me^.position.h + PlayerSpeed;
  142.                 me^.speed.h := PlayerSpeed;
  143.                 me^.face := rightFaces[BitAnd(me^.mode div 3, 3)]; {replace the face}
  144.             end;
  145.  
  146.         {  Not JUMP - not pushing key num 8 ,to prevent auto jumping!}
  147.         if (not (IsPressed($7E) or IsPressed($5B))) then
  148.             me^.JumpKey := NotPushed;
  149.  
  150.         {     JUMP - Key Number 8             }
  151.         if (IsPressed($7E) or IsPressed($5B)) then
  152.             begin
  153.                 if (me^.speed.v < 10) then
  154.                     begin
  155.                         if ((me^.JumpKey = NotPushed) and ((me^.action <> Jump) and (me^.action <> JumpFromHMov))) then
  156.                             begin
  157.                                 me^.speed.v := -JumpSpeed;
  158.                                 if (me^.action = StandOnHMovPlatform) then
  159.                                     begin
  160.                                         me^.position.h := me^.position.h + me^.speed.h;
  161.                                         me^.action := JumpFromHMov;
  162.                                     end
  163.                                 else
  164.                                     me^.action := Jump;
  165.  
  166.                                 me^.JumpKey := Pushed;
  167.                             end;
  168.                     end;
  169.             end;
  170.             { Down - Key Number 2}
  171.         if (IsPressed($7D) or IsPressed($54)) then
  172.             ;
  173.  
  174.             {         - Clear Key}
  175.         if (IsPressed($47)) then
  176.             ;
  177.     end;
  178.  
  179. end.